package com.park.map;

import androidx.activity.result.ActivityResultLauncher;
import androidx.appcompat.app.AppCompatActivity;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import com.google.zxing.integration.android.IntentIntegrator;
import com.journeyapps.barcodescanner.ScanContract;
import com.journeyapps.barcodescanner.ScanOptions;

import pub.devrel.easypermissions.AfterPermissionGranted;
import pub.devrel.easypermissions.EasyPermissions;

public class MainActivity extends AppCompatActivity {
    public static final int RESULT_CODE = 1001;
    /**
     * 扫码回调
     */
    private final ActivityResultLauncher<ScanOptions> barcodeLauncher = registerForActivityResult(new ScanContract(),
            result -> {
                //判断是否扫码
                if (result.getContents() == null) {
                    Toast.makeText(MainActivity.this, "Cancelled", Toast.LENGTH_LONG).show();
                } else {
                    Toast.makeText(MainActivity.this, "Scanned: " + result.getContents(), Toast.LENGTH_LONG).show();
                }
                //跳转到地图页面
                Intent intent = new Intent(MainActivity.this, MapActivity.class);
                startActivity(intent);
            });

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //申请权限
        methodRequiresTwoPermission();
    }

    //动态权限框架
    @AfterPermissionGranted(RESULT_CODE)
    private void methodRequiresTwoPermission() {
        //申请权限返回
        String[] perms = {Manifest.permission.INTERNET, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA};
        if (EasyPermissions.hasPermissions(MainActivity.this, perms)) {
            // 已有权限
            barcodeLauncher.launch(new ScanOptions());

        } else {
            //没有权限，再次申请
            EasyPermissions.requestPermissions(MainActivity.this, "请求获取您的位置信息、本地读写和相机权限",
                    RESULT_CODE, perms);
        }
    }
    public void clickScan(View view){
        //调起扫码界面
        barcodeLauncher.launch(new ScanOptions());
    }

}